<?php
namespace logic\admin\shop;
use library\common\collections\ArrayUtils;
use model\ShopGoods;
use model\ShopGoodsCategory;

/**
 * User: lsx
 * Date: 2017/6/2
 * Time: 10:28
 * Progress:0%
 */
class LShopGoodsCategory
{
    private $shopGoodsCategory;
    public function __construct()
    {
        $this->shopGoodsCategory=new ShopGoodsCategory();
    }

    //类别列表
    public function index_data($param){
        $model = new ShopGoodsCategory();
        $where=array();

        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['a.name|ss.name'] = array('like', "%{$keywords}%"); //查询条件
        }

        $status = $param['status'];
        if ($status > -1) {
            $where['flag_use'] = $status;
        }

        $store = $param['store'];
        if ($store > -1) {
            $where['store_id'] = $store;
        }

        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['sort'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['ss.id'] = 'desc'; //默认排序
            $sort['a.id'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model->alias('a')->join('shop_store ss','ss.id=a.store_id','left')->field('a.*,ss.name sname')
            ->where($where)
            ->order($sort)
            ->limit($start, $page_size)
            ->select();

        //统计每个分类下的商品个数
        $model_sg=new ShopGoods();
        foreach ($rows as $key => $val){
            $rows[$key]['goods_num'] = $model_sg->where(['store_id'=>$val['store_id'],'category_tree'=> $val['id'],'flag_delete'=>0])->count();
        }

        //记录总数
        $total = $model->alias('a')->join('shop_store ss','ss.id=a.store_id','left')
            ->where($where)->count();


        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );
        return $array;
    }

    public function logic_index_data(){
        $data=$this->shopGoodsCategory->where(['flag_delete'=>0])->select();
        $list_tree = $this->tree_asc($data);
        return $list_tree;
    }

    protected function tree_asc($list, $p_id = 0) {
        $list_tree = array();
        foreach ($list as $menu) {
            if ($menu['parent_id'] == $p_id) {

                if (!in_array($menu, $list_tree)) {
                    array_push($list_tree, $menu);
                }
                $list_son = $this->tree_asc($list, $menu['id']);
                if ($list_son) {
                    foreach ($list_son as $menu_son) {
                        array_push($list_tree, $menu_son);
                    }
                }
            }
        }
        return $list_tree;
    }

    public function logic_insert($param){
        $res=$this->shopGoodsCategory->insert($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>"添加成功"
            );
        }else{
            return array(
                'success'=>false,
                'message'=>"添加失败"
            );
        }
    }

    public function logic_edit($id){
        $item=$this->shopGoodsCategory->where(['id'=>$id])->find();
        $item=$item->getData();
        $result =$this->shopGoodsCategory->where(['id'=>$item['parent_id']])->field("name as parent_name")->find();

        if($result!=="" && $result!==null){
            $parent = $result->getData();
            $item['parent_name'] =  $parent['parent_name'];
        }else{
            $item['parent_name'] = null;
        }
        return $item;
    }

    public function logic_update($param){
        $res=$this->shopGoodsCategory->update($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>"修改成功"
            );
        }else{
            return array(
                'success'=>false,
                'message'=>"修改失败"
            );
        }
    }

    public function logic_delete($param){
        $res1=$this->shopGoodsCategory->where(['id'=>$param['id']])->delete();
      //  $c_id=$this->shopGoodsCategory->where(['store_id'=>$param['store_id'],'isdefault'=>1])->field('id')->find();
        //把该分类下的所有商品的分类改为"未分组"
        $model_sg=new ShopGoods();
        $res2 = $model_sg->where(['store_id'=>$param['store_id'],'category_tree'=> $param['id']])->update(['category_tree'=>0]);

        if($res1 !== false && $res2 !== false){
            return array(
                'success'=>true,
                'message'=>"删除成功"
            );
        }else{
            return array(
                'success'=>false,
                'message'=>"删除失败"
            );
        }
    }

    public function logic_cate_treeview_data(){
        $where['flag_use'] = 1;
        $where['flag_delete'] = 0;
        $result = $this->shopGoodsCategory->where($where)->field("id,parent_id,name as text")->order('sort asc')->select();
        $list = json_decode(json_encode($result),true);
        return ArrayUtils::arrayToTree($list,'id','parent_id','nodes','0')['nodes'];
    }

}